Membuat Daftar Alamat Pribadi
Berikut ini akan dijelaskan cara membuat daftar alamat pribadi, dengan
menggunakan php dengan database MySql. Sebelumnya buat dulu tabel dengan
membuat file tabel.sql dengan isi sebagai berikut :
CREATE TABLE alamat (
Nama varchar(80) NOT NULL,
Alamat varchar(255),
Telepon varchar(255),
ID INT NOT NULL auto_increment,
PRIMARY KEY (ID)
);
Setelah itu dari dos prompt ketikkan perintah :
#mysql namadatabase -uusername -ppassword<tabel.sql
Nah sekarang anda telah mempunyai sebuah tabel bernama alamat dengan
beberapa fieldnya, karena bersifat personal maka sebelum mengakses personal
address book, kita buat dulu halaman login sebagai berikut :
<form action="login.php" method="post">
<center>
<table width="305">
<tr>
<td width="50">Username</td>
<td width="5">:</td>
<td width="250"><input type="text" name="username" size="12"
maxlength="8"></td>
</tr>
<tr>
<td width="50">Password</td>
<td width="5">:</td>
<td width="250"><input type="password" name="userpass" size="12"
maxlength="8"></td>
</tr>
<tr>
<td colspan="3">
<input type="Submit" value="Submit">
<input type="Reset" value="Reset">
</td>
</tr>
</table>
Lembar 17 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
</center>
</form>
Dan sekarang kita membuat halaman validasinya (login.php) :
<?
//pengecekan login
if ($username == "username")
{
if ($userpass == "userpassword")
{
//set cookie ketika login sukses dan cookie di set aktif dalam waktu 1
jam
setcookie("usernames",$username, time()+3600);
//me-redirect ke halaman selanjutnya
Header("Location: nextadd.php?mul=0");
}
else
{
echo ("<center><font face='arial' size='3' color='red'>Invalid
Password!!!</font><br> <a href='index.html'>login lagi</a></center>");
}
}
else
{
echo ("<center><font face='arial' size='3' color='red'>Invalid Username!!!
</font><br> <a href='index.html'>login lagi</a></center>");
}
?>
Untuk koneksi dengan database, kita buat file connector.txt dengan isi sebagai
berikut :
<?
// buka koneksi ke database server
$database="namadatabase";
$hostname="namahost";
$username="username";
$password="password";
if (!$dbh=mysql_connect($hostname,$username,$password))
{
echo mysql_error();
exit;
}
// select default database
mysql_select_db($database, $dbh);
?>
Lembar 18 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
Untuk menampilkan daftar alamat kita dengan kemampuan untuk edit data dan
menghapus beberapa record sekaligus dapat kita lihat pada halaman nextadd.php
berikut :
<?//cek apakah cookie masih valid
if ($usernames != "username")
{
Header("Location: index.html");
}
?>
<html>
<head>
<title>My Address Book</title>
</head>
<script language="JavaScript">
<!--
//fungsi buat dropdown menu
function fungsimu(){
if
(document.milih.mul.options[document.milih.mul.selectedIndex].value !=
"none")
{
location =
document.milih.mul.options[document.milih.mul.selectedIndex].value
}}
// fungsi validasi buat search
function submit_search() {
if (document.cari.keywrd.value=="") {
alert( "Insert Your Keywords !" )
document.cari.keywrd.focus();
return false}
else
document.cari.submit();
}
//-->
</script>
<body>
<br>
<font face="Verdana,Arial" size="2">
<form name="cari" action="cari.php?mul=0" method="post" onsubmit="return
submit_search();">
<?
include "connector.txt";
// menghitung jumlah record
$sqlcnt="select count(id) as recort from alamat";
if(!$res2=mysql_query($sqlcnt,$dbh))
{
echo mysql_error();
Lembar 19 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
exit;
}
$rows=mysql_fetch_array($res2);
$recort=$rows["recort"];
// previous navigation
$lanjuts=$mul-20;
if ($lanjuts > 0)
{
if ($lanjuts == 1)
{
echo "<a href='nextadd.php?mul=0'>Previous</a>";
}
else
{
echo "<a href='nextadd.php?mul=".$lanjuts."'>Previous</a>";
}
}
// next navigation
$lanjut=$mul+20;
if ($lanjut < $recort)
{
if ($mul == 0)
{
$lanjut=$mul+21;
echo "<a href='nextadd.php?mul=".$lanjut."'>Next</a>";
}
else
{
$lanjut=$mul+20;
echo "<a href='nextadd.php?mul=".$lanjut."'>Next</a>";
}
}
?>
<a href="address.php">Add Address</a>
Search for :
<input type="text" name="keywrd" size="20" maxlength="50">
<input type="Submit" value="Search">
<a href="hapus.php">Logout</a>
</font><br>
</form>
<form action="nextadd.php" name="milih" method="post">
<font face="verdana,arial" size="2">Page
<select name="mul" onchange="fungsimu()">
Lembar 20 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
<? //page navigation
$habag=floor($recort/20);
$habag=$habag+1;
$c=1;
$moel=1;
while ($c<=$habag):
if ($c == 1)
{
if ($mul == 0)
{
echo "<option value='nextadd.php?mul=0' selected>".$c."</option>";
}
else
{
echo "<option value='nextadd.php?mul=0'>".$c."</option>";
}
}
else
{
$moel=$moel+20;
if ($mul == $moel)
{
echo "<option value='nextadd.php?mul=".$moel."'
selected>".$c."</option>";
}
else
{
echo "<option value='nextadd.php?mul=".$moel."'>".$c."</option>";
}
}
$c++;
endwhile;
?>
</select>
of
<?
echo $habag;
echo " Pages";
?>
</font>
</form>
<font face="Arial" size="2">
<?
$ent=$mul+19;
if ($mul == 0)
{
echo "Showing 1 - 20 of ".$recort." records";
}
else
{
if ($ent > $recort)
{
echo "Showing ".$mul." - ".$recort." of ".$recort." records";
}
else
{
Lembar 21 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
echo "Showing ".$mul." - ".$ent." of ".$recort." records";
}
}
?> </font>
<form action="hapusadd.php" method="post">
<table border="1" borderlight="Black" borderdark="Black" cellspacing="1">
<tr>
<td width="150" align="center"><font face="Arial"
size="3"><b>Nama</b></font></td>
<td width="300" align="center"><font face="Arial"
size="3"><b>Alamat</b></font></td>
<td width="150" align="center"><font face="Arial" size="3"><b>No.
Telepon</b></font></td>
<td width="50" align="center"><font face="Arial"
size="3"><b>Edit</b></font></td>
<td width="10" align="center"><font face="Arial"
size="3"><b>Hapus</b></font></td>
</tr>
<? //menampilkan 20 rekord per halaman
$sqladd="select Nama, Alamat, Telepon, ID from alamat order by Nama limit
$mul,20";
//execute query
if(!$res=mysql_query($sqladd,$dbh))
{
echo mysql_error();
exit;
}
while ($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td width='150'><font face='Arial'
size='2'>".$row["Nama"]."</font></td>";
if ($row["Alamat"] != '')
{
echo "<td width='300'><font face='Arial'
size='2'>".$row["Alamat"]."</font></td>";
}
else
{
echo "<td width='300'> </td>";
}
if ($row["Telepon"] != '')
{
echo "<td width='150'><font face='Arial'
size='2'>".$row["Telepon"]."</font></td>";
}
else
{
echo "<td width='150'> </td>";
}
echo "<td width='50' align='center'><a
href='editadd.php?det=".$row["ID"]."'><font face='arial'
size='2'>Edit</font></a></td>";
Lembar 22 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
echo "<td width='10' align='center'><input type='checkbox' name='items[]'
value='".$row["ID"]."'></td>";
}
?>
</table>
<input type="Submit" value="Delete">
</form>
Berikut ini adalah halaman untuk menambah data (address.php) :
<?//cek cookie
if ($usernames != "username")
{
Header("Location: index.html");
}
?>
<form name="tambah" action="tbaddress.php" method="post" onsubmit="return
submit_search2();">
<table width="500">
<tr>
<td width="80"><font face="Arial">Nama</font></td>
<td width="5">:</td>
<td width="345"><input type="text" name="nama" size="20"
maxlength="50"></td>
</tr>
<tr>
<td width="80"><font face="Arial">Alamat</font></td>
<td width="5">:</td>
<td width="345"><textarea name="alamat" rows="4" cols="30"></textarea></td>
</tr>
<tr>
<td width="80"><font face="Arial">No. Telp</font></td>
<td width="5">:</td>
<td width="345"><textarea name="telepon" rows="3"
cols="30"></textarea></td>
</tr>
</table>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
File tbaddress.php berisi sebagai berikut :
<?
//cek cookie
if ($usernames != "username")
{
Header("Location: index.html");
}
Lembar 23 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
include "connector.txt";
//memasukkan data ke database
//mengganti karakter ' dengan `
//mengganti input enter dengan tag <br>
$namas=(eregi_replace("'","`", $nama));
$alamats=(eregi_replace("'","`", $alamat));
$alamats=(eregi_replace( "\n" ,"<br>", $alamat));
$telepons=(eregi_replace("'","`", $telepon));
$telepons=(eregi_replace("\n","<br>", $telepon));
$sqlins="insert into alamat (Nama, Alamat, Telepon) values ('$namas',
'$alamats', '$telepons')";
if(!$res=mysql_query($sqlins,$dbh))
{
echo mysql_error();
exit;
}
?>
<html>
<head>
<title>Tambah Address Book</title>
</head>
<body>
<font face="verdana, Arial" size="3">Your Data Has Been Added</font><br>
<a href="nextadd.php?mul=0">Address List</a>
<br>
</font>
</form>
</body>
</html>
Untuk menghapus beberapa record sekaligus, berikut ini adalah caranya (file
hapusadd.php yang merupakan hasil post dari file nextadd.php) :
<?
//cek cookie
if ($usernames != "username")
{
Header("Location: index.html");
}
// jangan lupa file include-nya
include "connector.txt";
// menghapus beberapa rekord sekaligus
for ($i=0; $i
{
if ($items[$i] != "")
{
$sqldel="delete from alamat where ID=$items[$i]";
}
Lembar 24 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
if (! mysql_query($sqldel,$dbh))
{
echo mysql_error();
exit;
}
}
?>
<html>
<head>
<title>Update Address Book</title>
</head>
<font face="verdana, Arial" size="3">Your Data Has Been Deleted</font><br>
<a href="nextadd.php?mul=0">Address List</a>
</body>
</html>
Sedangkan untuk menampilkan rekord/data hasi pencarian adalah pada file cari.php
dengan isi sebagai berikut :
<?//cek apakah cookie masih valid
if ($usernames != "username")
{
Header("Location: index.html");
}
?>
<html>
<head>
<title>Search Results</title>
</head>
<body>
<?
include "connector.txt";
//menghitung jumlah record hasil search
$sqlcnt="select count(id) as recort from alamat where Nama like '%$keywrd%'
or Alamat like '%$keywrd%' or Telepon like '%$keywrd%'";
if(!$res2=mysql_query($sqlcnt,$dbh))
{
echo mysql_error();
exit;
}
$rows=mysql_fetch_array($res2);
$recort=$rows["recort"];
// previous navigation
$lanjuts=$mul-20;
if ($lanjuts > 0)
Lembar 25 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
{
if ($lanjuts == 1)
{
echo "<a href='cari.php?mul=0&keywrd=".$keywrd."'>Previous</a>";
}
else
{
echo "<a
href='cari.php?mul=".$lanjuts."&keywrd=".$keywrd."'>Previous</a>";
}
}
// next navigation
$lanjut=$mul+20;
if ($lanjut < $recort)
{
if ($mul == 0)
{
$lanjut=$mul+21;
echo "<a href='cari.php?mul=".$lanjut."&keywrd=".$keywrd."'>Next</a>";
}
else
{
$lanjut=$mul+20;
echo "<a href='cari.php?mul=".$lanjut."&keywrd=".$keywrd."'>Next</a>";
}
}
?> <br>
<font face="Arial" size="2"><b>Searching for "<? echo $keywrd; ?>" results
:</b></font><br> <br>
<font face="Arial" size="2"><b>
<?
$ent=$mul+19;
if ($recort != 0)
{
if ($mul == 0)
{
if ($recort < 20)
{
echo "Showing 1 - ".$recort." of ".$recort." results";
}
else
{
echo "Showing 1 - 20 of ".$recort." results";
}
}
else
{
if ($ent > $recort)
{
echo "Showing ".$mul." - ".$recort." of ".$recort." records";
}
else
Lembar 26 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
{
echo "Showing ".$mul." - ".$ent." of ".$recort." records";
}
}
?>
</b></font>
<?
//menampilkan hasil pencarian sebanyak 20 record
$sqlsearch="select Nama, Alamat, Telepon, ID from alamat where Nama like
'%$keywrd%' or Alamat like '%$keywrd%'
or Telepon like '%$keywrd%' order by Nama limit $mul,20";
//execute query
if(!$res=mysql_query($sqlsearch,$dbh))
{
echo mysql_error();
exit;
}
?>
<table border="1" borderlight="Black" borderdark="Black" cellspacing="1">
<tr>
<td width="150" align="center"><font face="Arial"
size="3"><b>Nama</b></font></td>
<td width="300" align="center"><font face="Arial"
size="3"><b>Alamat</b></font></td>
<td width="150" align="center"><font face="Arial" size="3"><b>No.
Telepon</b></font></td>
</tr>
<?
while ($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td width='150'><font face='Arial'
size='2'>".$row["Nama"]."</font></td>";
if ($row["Alamat"] != '')
{
echo "<td width='300'><font face='Arial'
size='2'>".$row["Alamat"]."</font></td>";
}
else
{
echo "<td width='300'> </td>";
}
if ($row["Telepon"] != '')
{
echo "<td width='150'><font face='Arial'
size='2'>".$row["Telepon"]."</font></td>";
}
else
{
echo "<td width='150'> </td>";
}
}
?>
</table>
<? }
Lembar 27 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
else
{
echo "No Matches Result Your Query";
}
?>
</body>
</html>
Untuk mengedit/update data dan logout dapat dapat dilihat pada script berikutnya.
Untuk mengedit / update data dilakukan dengan file editadd.php yang berupa form,
dengan isi sebagai berikut :
<?
//cek cookie
if ($usernames != "username")
{
Header("Location: index.html");
}
<html>
<head>
<title>Edit Address Book</title>
</head>
<script language="JavaScript">
<!--
function submit_search2() {
if (document.tambah.nama.value=="") {
alert( "Name is Required !" )
document.tambah.nama.focus();
return false}
else
document.cari.submit();
}
</script>
<body>
<? include "connector.txt";
//query selected record
$sqled="select Nama, Alamat, Telepon, ID from alamat where ID=$det";
if(!$res=mysql_query($sqled,$dbh))
{
echo mysql_error();
exit;
}
Lembar 28 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
$row=mysql_fetch_array($res);
$alamats=$row["Alamat"];
$alamats=(eregi_replace( "<br>" ,"\n", $alamats));
$telepons=$row["Telepon"];
$telepons=(eregi_replace( "<br>" ,"\n", $telepons));
?>
<form name="tambah" action="upadd.php" method="post" onsubmit="return
submit_search2();">
<table width="500">
<tr>
<td width="80"><font face="Arial">Nama</font></td>
<td width="5">:</td>
<td width="345"><input type="text" name="nama" size="20" maxlength="50"
value='<?echo $row["Nama"];?>'></td>
</tr>
<tr>
<td width="80"><font face="Arial">Alamat</font></td>
<td width="5">:</td>
<td width="345"><textarea name="alamat" rows="4" cols="30"><? echo
$alamats; ?></textarea></td>
</tr>
<tr>
<td width="80"><font face="Arial">No. Telp</font></td>
<td width="5">:</td>
<td width="345"><textarea name="telepon" rows="3" cols="30"><? echo
$telepons; ?></textarea></td>
</tr>
</table>
<input type="hidden" name="ids" value='<?echo $det; ?>'>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
Untuk melalukan updating data dilakukan oleh file upadd.php berisi sebagai berikut :
<?
//cek cookie
if ($usernames != "username")
{
Header("Location: index.html");
}
include "connector.txt";
//memasukkan data ke database
//mengganti karakter ' dengan `
//mengganti input enter dengan tag <br>
$namas=(eregi_replace("'","`", $nama));
$alamats=(eregi_replace("'","`", $alamat));
$alamats=(eregi_replace( "\n" ,"<br>", $alamat));
Lembar 29 / 36
D oc,reading report; Web Programming ; wildanthoyib; thoyib@gmail.com ®
$telepons=(eregi_replace("'","`", $telepon));
$telepons=(eregi_replace("\n","<br>", $telepon));
$sqlins="Update alamat set Nama='$namas', Alamat='$alamats',
Telepon='$telepons' where ID=$ids";
if(!$res=mysql_query($sqlins,$dbh))
{
echo mysql_error();
exit;
}
?>
<html>
<head>
<title>Update Address Book</title>
</head>
<body>
<font face="verdana, Arial" size="3">Your Data Has Been Updated</font><br>
<a href="nextadd.php?mul=0">Address List</a>
</body>
</html>
Sedangkan buat log out kita hapus cookie kita dengan file hapus.php sebagai
berikut :
<? //delete cookie
setcookie("usernames");
Header("Location: index.html");
?>
Sekarang anda telah dapat membuat buku tamu pribadi